class Solution {
public:
    vector<char> result;
    bool check(int n) {
        return (n & (n - 1)) == 0;
    }
    bool dfs(string &s, vector<bool> &vis) {
        if (result.size() == 1 && result[0] == '0') return false;
        if (result.size() == s.size()) {
            int num = 0;
            for (int i = 0; i < result.size(); i++) {
                num = num * 10 + result[i] - '0';
            }
            return check(num);
        }
        for (int i = 0; i < s.size(); i++) {
            if (vis[i]) continue;
            vis[i] = true;
            result.push_back(s[i]);
            int ans = dfs(s, vis);
            if (ans == true) return true;
            vis[i] = false;
            result.pop_back();
        }
        return false;
    }
    bool reorderedPowerOf2(int n) {
        if (n == 1) return true;
        string str = to_string(n);
        vector<bool> vis(10);
        return dfs(str, vis);
    }
};
